package com.example.exam.service.impl;



import com.example.commonutils.ResultData;
import com.example.exam.dto.StuSubmitExamDTO;
import com.example.exam.entity.Exam;
import com.example.exam.entity.Topic;
import com.example.exam.entity.UserGrade;
import com.example.exam.entity.UserTopic;
import com.example.exam.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;

@Service
@Transactional  //事务的注解
public class MarkExamServiceImpl implements MarkExamService {
    @Autowired
    private TopicService topicService;

    @Autowired
    private UserTopicService userTopicService;

    @Autowired
    private ExamService examService;

    @Autowired
    private UserGradeService userGradeService;

    @Override
    public ResultData submitTestPaper(StuSubmitExamDTO stuSubmitExamDTO, String userId) {
        try{
            UserGrade userGrade=new UserGrade();
            BeanUtils.copyProperties(stuSubmitExamDTO,userGrade);
            userGrade.setAnswerDate(new Date());
            userGrade.setUserId(userId);
            userGrade.setMarkStatus(0);
            userGrade.setExamStatus(1);

            double grade = 0;

            //总分
            for (UserTopic ut:stuSubmitExamDTO.getUserTopicList()){

                Topic topic = topicService.getById(ut.getTopicId());

                /*判断回答是否正确*/
                //填空题
                if(topic.getTopicType() == 3) {
                    String[] correctAnswerArr = topic.getCorrectAnswer().split("\n");
                    String[] userAnswerArr = ut.getUserAnswer().split("\n");
                    double topicGrade = 0;
                    for(int i=0;i<correctAnswerArr.length;i++){
                        if(correctAnswerArr[i].equals(userAnswerArr[i])){
                            topicGrade = topicGrade + (int) (topic.getScore() / correctAnswerArr.length);
                        }
                    }

                    ut.setUserScore(topicGrade);
                    grade = grade + topicGrade;
                }
                //简答题
                else if(topic.getTopicType() == 4){
                    String[] correctAnswerArr = topic.getCorrectAnswer().split("\n");
                    double topicGrade = 0;
                    for (String ca : correctAnswerArr) {
                        if(ut.getUserAnswer().contains(ca)){
                            topicGrade = topicGrade +  (int) (topic.getScore() / correctAnswerArr.length);
                        }
                    }
                    ut.setUserScore(topicGrade);
                    grade = grade + topicGrade;

                //其他题型
                }else if(topic.getCorrectAnswer().equals(ut.getUserAnswer())){
                    //评判分数
                    ut.setUserScore((double)topic.getScore());
                    //计算总分
                    grade = grade + topic.getScore();
                }else{
                    ut.setUserScore(0.0);
                }

                //设置为未批改状态
                ut.setTopicStatus("0");
                ut.setUserId(userId);
                userTopicService.save(ut);
            }
            userGrade.setGradeAuto(grade);
//            System.out.println(userGrade);

            Exam exam = examService.getById(stuSubmitExamDTO.getExamId());
            if (exam.getAutoMack() == 1){
                userGrade.setGrade(grade);
            }

            userGradeService.save(userGrade);
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            //强制手动事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultData.error().message("请求失败");
        }
    }
}
